# !/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@作者 杨阳
@时间 2018-06-21
@说明 处理权限相关的请求
@注意事项
"""

from flask import Blueprint, request

from model.SystemModel import TResourceModel
from util import LogUtil

resource = Blueprint('resource', __name__, url_prefix='/resource')


@resource.route("/get/<id>", methods=["GET"])
def get_resource(id):
    """
    根据权限id获得权限的详细信息
    :param id:  权限id
    :return:
    """
    return TResourceModel.get_resource_byid(id)


@resource.route("/tree/", methods=["POST"])
def resource_tree():
    """
    查询权限树
    :return: json 字符串
    """
    parent_id = request.form.get("parent_id")
    LogUtil.info("父节点id：{}".format(parent_id))
    return TResourceModel.get_resource_tree(parent_id)


@resource.route("/delete/<id>", methods=["DELETE"])
def delete_resource(id):
    """
    删除权限
    :param id:  权限id
    :return:
    """
    return TResourceModel.delete_resource(id=id)


@resource.route("/save/", methods=["POST"])
def save_resource():
    """
    增加或更新权限
    :return:
    """
    id = request.form.get("id")
    name = request.form.get("name")
    url = request.form.get("url")
    tindex = request.form.get("tindex")
    icon = request.form.get("icon")
    parentid = request.form.get("parentid")

    if id and "" != id:
        return TResourceModel.update_resource(id=id, name=name, url=url, tindex=tindex, icon=icon)
    else:
        return TResourceModel.add_resource(name=name, url=url, tindex=tindex, icon=icon, parentid=parentid)
